package com.vae.easternzhejiangbasin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vae.easternzhejiangbasin.domain.entity.LoginUser;
import com.vae.easternzhejiangbasin.domain.entity.User;
import com.vae.easternzhejiangbasin.enums.AppHttpCodeEnum;
import com.vae.easternzhejiangbasin.exception.SystemException;
import com.vae.easternzhejiangbasin.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author LZH
 * @date 2023/2/16
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, username);
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
        }
        return new LoginUser(user);
    }
}
